<?php

/**
 * Description of MenuController
 *
 * @author gun
 */
require_once 'BaseController.php';
class Admin_MenuController extends Admin_BaseController {
    
    public function init() {
        parent::init();
    }
    
    public function indexAction() {
        $request = $this->getRequest();

        if ($this->httpRequest()->isXmlHttpRequest()) {
            $q = $request->getParam('q');
            $page = $request->getParam('page');
            
            $where = array('name LIKE ?'=>"%$q%");
            $stat = $this->_helper->ListPage('Admin_Model_MenuService', $page, $where);            
            
            $options = array(
                'limit'=>
                    array(
                        $stat['perpage'] => $stat['offset'])
            );
            
            $service = AppRegistry::loadService('Admin_Model_MenuService');
            $data = $service->findAllMenu($where, $options);
            
            if ($data) {
                $this->_helper->json(array(
                    'data' => $data,
                    'pview'=> $stat['pview'],
                    'offset'=> $stat['offset'],
                    'fieldname'=> 'gmenuID'
                    ));
                
            } else {
                $this->_helper->json(array('error'=>'No Data Found.'));
            }
        }
    }        
    
    public function addAction() {        
        $this->view->groupmenu = $this->_helper->CatList(array(array('gmenuID'=>'top', 'name'=>'Top')));

    }
    
    public function lookUpParentMenuAction() {
        $groupMenu = $this->getParam('gmenuID');
        
        $service = AppRegistry::loadService('Admin_Model_MenuService');
        $data = $service->lookupParentMenu( $groupMenu );

        if ($data)
            $parent = $this->_helper->CatList($data);
        else
            $parent = array();
        
        $this->_helper->json(array('menuparent' => $parent));        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function editAction() {
        $id = $this->getParam('id');
        
        $where = array('menuID = ?' => $id);
        $service = AppRegistry::loadService('Admin_Model_MenuService');
        $data = $service->findRow($where);
        
        $groupMenu = array
                        (
                            0 => array
                                (
                                    'gmenuID' => 'top',
                                    'name' => 'Top'
                                )
                        );
        
        $parentMenu = $service->lookupParentMenu( $data['gmenuID'] );
        
        $this->view->groupmenu = $this->_helper->CatList($groupMenu, $data['gmenuID']);
        $this->view->parentmenu = $this->_helper->CatList($parentMenu, $data['parentID']);
        $this->view->data = $data;
    }
    
    public function saveAction() {
        $id = $this->getParam('id');
        $gmenu = $this->getParam('groupmenu');
        $name = $this->getParam('name');
        $link = $this->getParam('link');
        $icon = $this->getParam('icon');
        $parent = $this->getParam('parentmenu');
                
        $data = array(
            'menuID'=>$id,
            'gmenuID'=>$gmenu,
            'parentID'=>$parent,
            'name'=>$name,
            'link'=>$link,
            'icon'=>$icon
        );        
        
        $baseUserInfo = $this->baseUserLogin($id);
        
        foreach($baseUserInfo as $key => $value) $data[ $key ] = $value;

        try {
            $service = AppRegistry::loadService('Admin_Model_MenuService');
            
            $save = (!isset($id) ? $service->doSave($data) : $service->doUpdate($data, $id)); 
            $this->_helper->json(array('status'=>($save ? 'Save Success.' : 'Nothing changed.')));            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getMessage()));
        }
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function deleteAction() {
        try {
            $id = $this->getParam('id');
            $service = AppRegistry::loadService('Admin_Model_MenuService');        
            $menuDelete = $service->doDelete($id);
            
            $aclService = AppRegistry::loadService('Admin_Model_AclService');
            $aclDelete = $aclService->doDeleteByMenuID($id);
            
            $this->_helper->json(array('status'=>(($menuDelete && $aclDelete) ? 'Delete success.' : 'Nothing deleted.')));
            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getTraceAsString()));
        }
    }

}
